#include "Acceptor.h"
#include <iostream>

using namespace std;
Acceptor::Acceptor(EventLoop* loop, const std::string& ip, 
        const unsigned short port): 
        loop_(loop)
{
    serverSock_ = new Socket{};
    serverSock_->setReusable();
    serverSock_->Bind(ip.c_str(), port);
    serverSock_->Listen();
    auto ch = new Channel(loop_->getEpPtr(), serverSock_->GetFd());
    ch->enableReading();
    ch->setReadCb(std::bind(&Acceptor::newConnection, this));
    loop_->updateChannel(ch);
}

Acceptor::~Acceptor()
{
    delete serverSock_;
    delete serverCh;
}
void Acceptor::newConnection() {
    SockAddrIn addr;
    auto clientSock = serverSock_->Accept(&addr);
    if (clientSock == nullptr) {
        return;
    }
    // 以上是技术层，下面调用用户自定义函数
    acceptCb_(clientSock);
}